﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cairo;

namespace FigureDraw.Graphic
{
    class CairoPNGGraphic : CommonGraphic
    {
        private string filename;
        private ImageSurface img;
        private Context c;

        public CairoPNGGraphic(string filename)
        {
            this.filename = filename;
            img = new ImageSurface(Format.Argb32, 800, 600);
            c = new Context(img);
        }

        override public void DrawLine(double x1, double y1, double x2, double y2)
        {
            c.MoveTo(x1, y1);
            c.LineTo(x2, y2);
            c.Stroke();
        }

        override public void DrawRectangle(double x, double y, double width, double height)
        {
            c.Rectangle(x, y, width, height);
            c.Stroke();
        }

        override public void DrawEllipse(double x, double y, double width, double height)
        {
            double ratio = Math.Min(width, height) / Math.Max(width, height);

            double wScale = width >= height ? 1 : ratio;
            double hScale = height >= width ? 1 : ratio;
            PointD center = new PointD((x + width / 2) / wScale, (y + height / 2) / hScale);

            c.Scale(wScale, hScale);
            c.Arc(center.X, center.Y, Math.Max(width, height) / 2, 0, 2 * Math.PI);
            c.Scale(1 / wScale, 1 / hScale);
            c.Stroke();
        }

        public override void DrawPicture(List<MyShape> shapes)
        {
            base.DrawPicture(shapes);
            img.WriteToPng(filename);
            img.Finish();
        }

        public override void DrawText(string text, double size, double x, double y)
        {
            throw new NotImplementedException();
        }
    }
}
